﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

namespace Jy123.Web.Friend
{
    public partial class RequestConfirm1 : System.Web.UI.Page
    {
        private int userID;
        protected void Page_Load(object sender, EventArgs e)
        {
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                Response.Redirect("/");
            }
            userID = int.Parse(strUserIdFromCookie);

            StringBuilder sbHtml;
            string type = Request["type"];
            if (!string.IsNullOrEmpty(type))
            {
                sbHtml = new StringBuilder(FunRquuest(Request["type"].ToString()));
                Response.ContentType = "text/plain";
                Response.Write(sbHtml.ToString());
            }
            else
            {

                #region 加载页面
                sbHtml = new StringBuilder(Cache["user_friendconfirm"].ToString());
                sbHtml = sbHtml.Replace("$Header$", Cache["user_header"].ToString());
                sbHtml = sbHtml.Replace("$Footer$", Cache["user_footer"].ToString());
                sbHtml = sbHtml.Replace("$LeftUserCmd$", Cache["user_navigation_up"].ToString());
                sbHtml = sbHtml.Replace("$LeftUserFriendAdd$", Cache["user_friendadd"].ToString());


                Jy123.BLL.User.Friend bllFrd = new Jy123.BLL.User.Friend();
                DataTable dtRequestList = bllFrd.FriendRequest(1, int.MaxValue, userID).Tables[0];
                string strRequestList = Cache["user_friendrequestlist"].ToString();
                StringBuilder sbRequestList = new StringBuilder();
                for (int i = 0; i < dtRequestList.Rows.Count; i++)
                {
                    string str = strRequestList;
                    //根据用户ID判断用户是否有头像,如果没有头像就返回默认头像地址
                    string userPortrait = new Jy123.BLL.User.SPace().DefaultPortrait(Convert.ToInt32(dtRequestList.Rows[i]["FUser"]));

                    str = str.Replace("$RequesterID$", dtRequestList.Rows[i]["ID"].ToString());
                    str = str.Replace("$RequesterUserID$", dtRequestList.Rows[i]["FUser"].ToString());
                    str = str.Replace("$RequesterUserName$", dtRequestList.Rows[i]["UserName"].ToString());
                    str = str.Replace("$RequesterPortrait$", userPortrait);

                    //显示请求者城市
                    string strCityID = dtRequestList.Rows[i]["City"].ToString();
                    if (!string.IsNullOrEmpty(strCityID))
                    { str = str.Replace("$RequesterCity$", new Jy123.BLL.User_Dict_Area().GetModel(Convert.ToInt32(strCityID)).Name); }
                    else
                    { str = str.Replace("$RequesterCity$", "未知地区"); }

                    sbRequestList.Append(str);
                }
                sbHtml = sbHtml.Replace("$FriendRequestList$", sbRequestList.ToString());
                sbHtml = sbHtml.Replace("$CountRequest$", new BLL.User.Friend().CountRequest(userID).ToString());
                #endregion
                Response.ContentType = "text/plain";
                Response.Write(sbHtml.ToString());
            }
        }
        /// <summary>
        /// 处理请求
        /// </summary>
        /// <param name="type"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        private string FunRquuest(string type)
        {
            StringBuilder sbreq = new StringBuilder();
            string strUserID = Request.QueryString["UserID"];
            int reqUserID = Convert.ToInt32(strUserID);
            string reqID = Request.QueryString["reqID"];
            Jy123.Model.User_Main mUser = new Jy123.Model.User_Main();
            Jy123.BLL.User_Main bllUser = new Jy123.BLL.User_Main();

            //Jy123.Model.User_Request mUserRequest = new Jy123.Model.User_Request();
            //Jy123.BLL.User_Request bllUserRequest = new Jy123.BLL.User_Request();
            Jy123.Model.User_Friend mUserFrd = new Jy123.Model.User_Friend();
            Jy123.BLL.User_Friend bllUserFrd = new Jy123.BLL.User_Friend();

            Jy123.Model.User_Main mUserForState = new Jy123.Model.User_Main();
            Jy123.Model.User_Main mUserFrdForState = new Jy123.Model.User_Main();
            Jy123.BLL.User_Main bllUserForState = new Jy123.BLL.User_Main();

            switch (type)
            {
                case "ok":
                    mUser = bllUser.GetModel(Convert.ToInt32(strUserID));
                    string sel = Request["fclass"];
                    sbreq.Append(Cache["user_friendrequestok"].ToString());
                    sbreq.Replace("$RequesterUserID$", strUserID);
                    sbreq.Replace("$RequesterUserName$", mUser.UserName);
                    //处理请求
                    mUserFrd = bllUserFrd.GetModel(Convert.ToInt32(reqID));
                    mUserFrd.State = 1;
                    mUserFrd.FFriendClass = 0;//默认好友分组
                    bllUserFrd.Update(mUserFrd);

                    //处理好友表
                    
                    //把他添加到我的好友列表
                    mUserFrd.FUser = userID;
                    mUserFrd.FFriend = reqUserID;
                    mUserFrd.State = 1;
                    mUserFrd.AddTime = DateTime.Now;
                    mUserFrd.FFriendClass = Convert.ToInt32(sel);//我选择的分组
                    bllUserFrd.Add(mUserFrd);

                    //加入动态
                    //Jy123.Model.User_Main mUserForState = new Jy123.Model.User_Main();
                    //Jy123.Model.User_Main mUserFrdForState = new Jy123.Model.User_Main();
                    //Jy123.BLL.User_Main bllUserForState = new Jy123.BLL.User_Main();
                    mUserForState = bllUserForState.GetModel(userID);
                    mUserFrdForState = bllUserForState.GetModel(reqUserID);
                    new Jy123.BLL.Public.State().CreateMakeFriendState(mUserForState,mUserFrdForState, (int)Jy123.BLL.Public.EnumConfig.StateActionType.MakeFriend);
                    //加入动态
                    break;
                case "cancel":
                    mUser = bllUser.GetModel(Convert.ToInt32(strUserID));                    

                    sbreq.Append(Cache["user_friendrequestcancel"].ToString());
                    sbreq.Replace("$RequesterUserID$", strUserID);
                    sbreq.Replace("$RequesterUserName$", mUser.UserName);
                    bllUserFrd.Delete(Convert.ToInt32(reqID));

                    //加入动态
                    
                    mUserForState = bllUserForState.GetModel(userID);
                    mUserFrdForState = bllUserForState.GetModel(reqUserID);
                    new Jy123.BLL.Public.State().CreateMakeFriendState(mUserForState, mUserFrdForState, (int)Jy123.BLL.Public.EnumConfig.StateActionType.RefuseFriend);
                    //加入动态
                    break;
            }
            return sbreq.ToString();
        }
        
    }
}
